package com.example.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @CreateTime 2025-02-23
 * @Description mybatis-plus自动填充
 */

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        LocalDateTime now = LocalDateTime.now();
        // 填充 createTm
        this.strictInsertFill(metaObject, "createTm", LocalDateTime.class, now);
        // 插入时也需要填充 updateTm（因为 fill = INSERT_UPDATE）
        this.strictInsertFill(metaObject, "updateTm", LocalDateTime.class, now);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 更新时填充 updateTm
        this.strictUpdateFill(metaObject, "updateTm", LocalDateTime.class, LocalDateTime.now());
    }
}
